// ===============================================
// toast
// 
// Create by Will on 2020/10/5 5:57 PM
// Copyright @data_food.All rights reserved.
// ===============================================

import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_base/core/utils/error_utils.dart';
import 'package:flutter_base/core/widget/toast/icon_text.dart';
import 'package:flutter_base/core/widget/toast/loading.dart';

import 'bf_const.dart';

final CoreKitToastInit = BotToastInit;

class CoreKitToast {
  static CancelFunc showLoading(
      String text, {
        ValueListenable<String> textListenable,
        BackButtonBehavior backButtonBehavior = BackButtonBehavior.ignore,
        crossPage = false,
        clickClose = false,
      }) {
    return BotToast.showCustomLoading(
      toastBuilder: (_) => LoadingWidget(
        text,
        textListenable: textListenable,
      ),
      clickClose: clickClose,
      ignoreContentClick: true,
      crossPage: crossPage,
    );
  }

  static CancelFunc showSuccess(
      String text, {
        crossPage = false,
        clickClose = false,
      }) {
    return showIconText(
      text,
      widgetBuilder: (_) => Icon(
        Icons.done,
        size: CoreKitConst.toastIconSize,
        color: Colors.white,
      ),
      crossPage: crossPage,
      clickClose: clickClose,
    );
  }

  static CancelFunc showFailure(
      String text, {
        crossPage = false,
        clickClose = false,
      }) {
    return showIconText(
      text,
      widgetBuilder: (_) => Icon(
        Icons.error_outline,
        size: CoreKitConst.toastIconSize,
        color: Colors.white,
      ),
      crossPage: crossPage,
      clickClose: clickClose,
    );
  }

  static CancelFunc showError(
      dynamic error, {
        crossPage = false,
        clickClose = false,
      }) {
    final errMessage = ErrorUtils.messageFrom(error);
    return showFailure(
      errMessage,
      crossPage: crossPage,
      clickClose: clickClose,
    );
  }

  static CancelFunc showIconText(
      String text, {
        WidgetBuilder widgetBuilder,
        crossPage = false,
        clickClose = false,
        duration = CoreKitConst.toastDuration,
      }) {
    return showWidget(
      IconTextWidget(widgetBuilder, text),
      crossPage: crossPage,
      clickClose: clickClose,
    );
  }

  static CancelFunc showWidget(
      Widget child, {
        crossPage = false,
        clickClose = false,
        duration = CoreKitConst.toastDuration,
      }) {
    return BotToast.showAnimationWidget(
      toastBuilder: (_) => SafeArea(
        child: Align(
          alignment: Alignment.center,
          child: child,
        ),
      ),
      animationDuration: CoreKitConst.toastAnimationDuration,
      // wrapToastAnimation:
      //     (AnimationController controller, CancelFunc _, Widget child) {
      //   return FadeAnimation(
      //     controller: controller,
      //     child: child,
      //   );
      // },
      crossPage: crossPage,
      clickClose: clickClose,
      duration: duration,
    );
  }
}

class CoreKitToastNavigatorObserver extends BotToastNavigatorObserver {}
